/**
 * 支付工具类
 * 支持微信小程序、APP(微信、支付宝)支付
 */
import { config } from './config.js'

/**
 * 统一支付接口
 * @param {Object} payParams - 支付参数
 * @param {String} payParams.payType - 支付类型：wxpay-微信, alipay-支付宝
 * @param {Object} payParams.orderInfo - 订单信息
 * @param {Function} onSuccess - 支付成功回调
 * @param {Function} onFail - 支付失败回调
 * @param {Function} onComplete - 支付完成回调(无论成功失败)
 */
export function unifiedPay(payParams, onSuccess, onFail, onComplete) {
  // 判断运行平台
  const platform = uni.getSystemInfoSync().platform;
  
  // 支付类型
  const payType = payParams.payType || 'wxpay';
  
  if (payType === 'wxpay') {
    // 微信支付
    wxPay(payParams, platform, onSuccess, onFail, onComplete);
  } else if (payType === 'alipay') {
    // 支付宝支付
    aliPay(payParams, platform, onSuccess, onFail, onComplete);
  } else {
    // 不支持的支付类型
    console.error('不支持的支付类型:', payType);
    if (typeof onFail === 'function') {
      onFail({
        errMsg: '不支持的支付类型',
        code: -1
      });
    }
    if (typeof onComplete === 'function') {
      onComplete({
        success: false,
        message: '不支持的支付类型'
      });
    }
  }
}

/**
 * 微信支付
 * @private
 */
function wxPay(payParams, platform, onSuccess, onFail, onComplete) {
  // 处理不同平台的微信支付
  if (platform === 'android' || platform === 'ios') {
    // App环境
    appWxPay(payParams, onSuccess, onFail, onComplete);
  } else {
    // 小程序环境
    mpWxPay(payParams, onSuccess, onFail, onComplete);
  }
}

/**
 * 小程序微信支付
 * @private
 */
function mpWxPay(payParams, onSuccess, onFail, onComplete) {
  const { orderInfo } = payParams;
  
  // 调用微信支付API
  uni.requestPayment({
    provider: 'wxpay',
    timeStamp: orderInfo.timeStamp,
    nonceStr: orderInfo.nonceStr,
    package: orderInfo.package,
    signType: orderInfo.signType,
    paySign: orderInfo.paySign,
    success: (res) => {
      console.log('微信支付成功:', res);
      if (typeof onSuccess === 'function') {
        onSuccess(res);
      }
    },
    fail: (err) => {
      console.log('微信支付失败:', err);
      if (typeof onFail === 'function') {
        onFail(err);
      }
    },
    complete: (res) => {
      if (typeof onComplete === 'function') {
        onComplete({
          success: res.errMsg === 'requestPayment:ok',
          message: res.errMsg
        });
      }
    }
  });
}

/**
 * App微信支付
 * @private
 */
function appWxPay(payParams, onSuccess, onFail, onComplete) {
  const { orderInfo } = payParams;
  
  // App环境的微信支付
  uni.requestPayment({
    provider: 'wxpay',
    orderInfo: JSON.stringify(orderInfo), // App环境需要传递完整订单信息字符串
    success: (res) => {
      console.log('App微信支付成功:', res);
      if (typeof onSuccess === 'function') {
        onSuccess(res);
      }
    },
    fail: (err) => {
      console.log('App微信支付失败:', err);
      if (typeof onFail === 'function') {
        onFail(err);
      }
    },
    complete: (res) => {
      if (typeof onComplete === 'function') {
        onComplete({
          success: res.errMsg === 'requestPayment:ok',
          message: res.errMsg
        });
      }
    }
  });
}

/**
 * 支付宝支付
 * @private
 */
function aliPay(payParams, platform, onSuccess, onFail, onComplete) {
  // 仅在App环境支持支付宝支付
  if (platform === 'android' || platform === 'ios') {
    const { orderInfo } = payParams;
    
    uni.requestPayment({
      provider: 'alipay',
      orderInfo: orderInfo, // 支付宝订单数据
      success: (res) => {
        console.log('支付宝支付成功:', res);
        if (typeof onSuccess === 'function') {
          onSuccess(res);
        }
      },
      fail: (err) => {
        console.log('支付宝支付失败:', err);
        if (typeof onFail === 'function') {
          onFail(err);
        }
      },
      complete: (res) => {
        if (typeof onComplete === 'function') {
          onComplete({
            success: res.errMsg === 'requestPayment:ok',
            message: res.errMsg
          });
        }
      }
    });
  } else {
    // 微信小程序环境下不支持支付宝支付，使用H5支付替代
    console.warn('当前平台不支持支付宝支付，将跳转到H5支付页面');
    
    // 跳转到支付页面
    uni.navigateTo({
      url: '/pages/payment/webview?payType=alipay&orderId=' + payParams.orderInfo.orderId
    });
  }
}

export default {
  unifiedPay
} 